package com.loves.admin.service.impl;

import com.loves.admin.mapper.ProduceMapper;
import com.loves.admin.service.ProduceService;
import com.loves.admin.vo.ProduceVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProduceServiceImpl implements ProduceService {

    @Resource
    private ProduceMapper produceMapper;

    @Override
    public List<ProduceVO> findList() {
       // Produce produce = new Produce.Builder().id("111").name("111").version("1.0").parentId("1").createDate(LocalDateTime.now()).createId("111").build();
       // produceMapper.insert(produce);
        List<ProduceVO> produceList = produceMapper.findList();
        List<ProduceVO> result = new ArrayList<>();
        Map<String, List<ProduceVO>> stringListMap = produceList.stream().collect(Collectors.groupingBy(ProduceVO::getParentId));
        stringListMap.forEach((key, values) -> {
            Optional<ProduceVO> optMinProduceVo = values.stream().min(Comparator.comparing(ProduceVO::getCreateDate));
            Optional<ProduceVO> optMaxProduceVo = values.stream()
                    .filter(produceVO -> produceVO.getCreateId().equals(optMinProduceVo.get().getCreateId()))
                    .max(Comparator.comparing(ProduceVO::getCreateDate));
            values = values.stream().peek(produceVO -> {
                if (optMaxProduceVo.isPresent() && produceVO.getId().equals(optMaxProduceVo.get().getId())) {
                    produceVO.setIdentification("原始版本");
                }
            }).collect(Collectors.toList());
            result.addAll(values);
        });
        return result;
    }
}
